/**
 * Created by wang on 16-7-1.
 */
//发送信息
var sendUrl;
//微信sdk
var timestamp;//必填，生成签名的时间戳
var nonceStr;//必填，生成签名的随机串
var signature;// 必填，签名，见附录1
var sharelink;//分享链接
var sharetitle;//分享标题
var shareimg;//分享图标
var sharecontent;//分享描述
//发送到分享界面
var sendShare_con;//诗句
var sendShare_poem_type;//5言7言
var sendShare_sens;//4句8句
var sendShare_title;//爱情或者友情
var sendShare_heads;//输入的内容
$(document).ready(function () {
    var poem_type = decodeURI(get_parameter("poem_type"));//5言7言
    var poem_content = decodeURI(get_parameter("poem_con"));
    var poem_sens = decodeURI(get_parameter("poem_sens"));//4句8句
    var poem_title = decodeURI(get_parameter("poem_title"));//标题
    var poem_heads = decodeURI(get_parameter("poem_heads"));//input输入内容
    //下一次分享跳转
    sendShare_con = poem_content;
    sendShare_poem_type = poem_type;
    sendShare_sens = poem_sens;
    sendShare_title = poem_title;
    sendShare_heads = poem_heads;
    sendUrl ='http://'+ window.location.host + '/poem/share.html?poem_type=5&poem_sens=' + poem_sens + '&poem_con=' + sendShare_con;
    // alert(sendUrl);
    var content;
    var words = 5;
    // location.replace(location.href.replace(location.search, ""));
    // console.log("poem_content" + poem_content);
    // console.log("poem_title" + poem_title);
    if (poem_title == '爱情') {
        $('#lover').addClass('weui_tab_bd_item_active');
        $('#tolover').val(poem_heads);
        content = 'lover';
    } else {
        $('#friendA').click();
        $('#tofriend').val(poem_heads);
        content = 'friend';
    }
    drawPoem(content, poem_type, poem_sens, poem_content);
    $("#lover_change,#friend_change").on("click", function () {
        var word = parseInt($("[name = poem]:checked").attr("value"));
        judge($(this), word);
    });

    $(".choseradio").change(function () {
        var word = parseInt($("[name = poem]:checked").attr("value"));
        var input_id = $('.weui_bar_item_on').attr('id');
        var input_id = input_id.substr(0, input_id.length - 1);
        judge($('#' + input_id + '_change'), word);
    });
    $('#love_share,#friend_share').click(function () {
        sharePoem();
    });
});
//写诗
function drawPoem(content, poem_type, poem_sens, poem_content) {
    var poem_arr = [];
    if (poem_type == 5) {
        $("." + content + "-senven-4").hide();
        $("." + content + "-senven-8").hide();
        if (poem_sens == 4) {
            $("." + content + "-five-8").hide();
            $("." + content + "-five-4").show();
            var weui_arr = $("." + content + "-five-4" + " li");
        }
        else if (poem_sens == 8) {
            $("." + content + "-five-4").hide();
            $("." + content + "-five-8").show();
            var weui_arr = $("." + content + "-five-8" + " li");
        }
        for (var i = 0; i < poem_content.length; i++) {
            $(weui_arr[i]).text(poem_content[i]);
        }
    } else if (poem_type == 7) {
        $("." + content + "-five-4").hide();
        $("." + content + "-five-8").hide();
        if (poem_sens == 4) {
            $("." + content + "-senven-8").hide();
            $("." + content + "-senven-4").show();
            var weui_arr = $("." + content + "-senven-4" + " li");
        } else if (poem_sens == 8) {
            $("." + content + "-senven-4").hide();
            $("." + content + "-senven-8").show();
            var weui_arr = $("." + content + "-senven-8" + " li");
        }
        for (var i = 0; i < poem_content.trim().length; i++) {
            $(weui_arr[i]).text(poem_content[i]);
        }
    }
}
//得到地址的数据
function get_parameter(param) {
    var query = window.location.search;
    var iStart = query.indexOf(param);
    var iLen = param.length;
    if (iStart == -1) {
        return "";
    }
    iStart += iLen + 1;
    var iEndAnd = query.indexOf("&", iStart);
    var iEndSplash = query.indexOf("/", iStart);
    if (iEndAnd == -1 && iEndSplash == -1) {
        return query.substring(iStart);
    } else if (iEndAnd > 0) {
        return query.substring(iStart, iEndAnd);
    } else if (iEndSplash > 0) {
        return query.substring(iStart, iEndSplash);
    }
}
//判断并访问
function judge(zj, words) {
    var sents;
    var contentVar = $('.weui_tab_bd_item_active').attr('id');
    var inputContent = $("#to" + contentVar).val();
    var words = parseInt($("[name = poem]:checked").attr("value"));
    if (inputContent.replace(/[ ]/g, "").length < 2 || inputContent.replace(/[ ]/g, "").length > 8) {
        $.alert("请输入2-8字的尊称");
        return;
    }
    if (inputContent.replace(/[ ]/g, "").length <= 4) {
        sents = 4;
    } else {
        sents = 8;
    }
    $.showLoading("鸭鸭在挥墨泼亳中...");
    var data = {
        'words': words,
        'sents': sents,
        'title': zj.attr('value'),
        'heads': inputContent.replace(/[ ]/g, ""),
        'times': 1,
        'url': encodeURIComponent(sendUrl)
    };

    console.log('data:' + data);
    console.log('当前地址：' + 'http://'+window.location.host);
    var post_data = JSON.stringify(data);
    $.ajax({
        url: 'http://kcloudtest.iflytek.com/zb/poem?data=' + post_data,
        // url: 'http://127.0.0.1:10063/zb/poem?data=' + post_data,
        type: 'get',
        success: function (data) {
            $.hideLoading();
            get_data = JSON.parse(data);
            //初始化分享参数
            timestamp = get_data.signaturedata.timestamp;
            nonceStr = get_data.signaturedata.nonceStr;
            signature = get_data.signaturedata.signature;
            sharelink = get_data.signaturedata.url;
            sharetitle = get_data.signaturedata.title;
            shareimg = get_data.signaturedata.img_url;
            sharecontent = get_data.signaturedata.content;
            // share();
            var poem_con = get_data.result_text.replace(/[\r\n]/g, "");
            sendShare_con = poem_con;
            sendShare_sens = sents;
            sendShare_poem_type = words;
            sendShare_title = zj.attr('value');
            sendShare_heads = inputContent.replace(/[ ]/g, "");
            sendUrl = 'http://'+window.location.host + '/poem/share.html?poem_type=' + words + '&poem_sens=' + sendShare_sens + '&poem_con=' + sendShare_con;
            drawPoem(contentVar, words, sents, poem_con);
        }, error: function (data) {
            $.hideLoading();
            $.alert("人气太旺,请稍后...");
            $.alert("鸭鸭词穷了~~换个词吧~");
        }
    })
}
//分享作诗
function sharePoem() {
    if (sendShare_con == '' || sendShare_sens == '') {
        $.alert("请作诗后，再分享");
        return;
    } else {
        window.location.href = "share.html?poem_type=" + sendShare_poem_type + "&poem_sens=" + sendShare_sens + "&poem_con=" + sendShare_con + "&poem_title=" + sendShare_title + "&poem_heads=" + sendShare_heads;
    }
}
//分享
function share() {
    console.log("时间戳：" + timestamp);
    console.log("签名随机：" + nonceStr);
    console.log("签名：" + signature);
    // alert("链接：" + sharelink);
    console.log("标题：" + sharetitle);
    console.log("描述：" + sharecontent);
    wx.config({
        debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: 'wx8ef01b828a087354', // 必填，公众号的唯一标识
        timestamp: timestamp, // 必填，生成签名的时间戳
        nonceStr: nonceStr, // 必填，生成签名的随机串
        signature: signature,// 必填，签名，见附录1
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareQZone'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    });
    var title = "关注躺倒鸭，回复“装逼”获得更多神器";
    var desc = "关注躺倒鸭，最牛逼的装逼";
    var url = sharelink;
    var img = 'http://open.weixin.qq.com/qr/code/?username=tangdaoya';

    wx.ready(function () {
        wx.checkJsApi({
            jsApiList: [
                'onMenuShareTimeline',
                'onMenuShareAppMessage'
            ]
        });
        wx.onMenuShareTimeline({
            title: sharetitle, // 分享标题
            link: sharelink, // 分享链接
            imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya', // 分享图标
            success: function () {
                alert('鸭鸭感谢你的分享')
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                alert('撤销');
            }
        });
        wx.onMenuShareAppMessage({
            title: sharetitle,
            desc: sharecontent,
            link: sharelink,
            imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya',
            trigger: function (res) {

            },
            success: function (res) {
                alert('已分享');
            },
            cancel: function (res) {
                alert('已取消');
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });

        wx.onMenuShareQQ({
            title: sharetitle, // 分享标题
            desc: sharecontent, // 分享描述
            link: sharelink, // 分享链接
            imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya', // 分享图标
            success: function () {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });

        wx.onMenuShareQZone({
            title: sharetitle, // 分享标题
            desc: sharecontent, // 分享描述
            link: sharelink, // 分享链接
            imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya', // 分享图标
            success: function () {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });
    });
    wx.error(function (res) {

    });
}